import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/widgets/button/common_button.dart';
import 'package:get/get.dart';


class CommonNormalDialog extends StatelessWidget {
  final String title;
  final String? message;
  final String? cancelText;
  final String? sureText;
  final VoidCallback? onCancel;
  final VoidCallback? onSure;
  final Widget? cancelWidget;
  final Widget? sureWidget;
  final bool showCancel;
  final bool barrierDismissible;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final Decoration? decoration;
  final Color? backgroundColor;
  final Widget? bodyWidget;

  //点击确定和取消是否关闭
  final bool autoDismiss;
  final String? nameSetting;
  final TextAlign? messageTextAlign;
  final TextStyle? titleTextStyle;

  const CommonNormalDialog(
      {super.key,
      this.title = "",
      this.message,
      this.cancelText,
      this.sureText,
      this.onCancel,
      this.onSure,
      this.showCancel = true,
      this.cancelWidget,
      this.sureWidget,
      this.barrierDismissible = true,
      this.autoDismiss = true,
      this.padding,
      this.margin,
      this.decoration,
      this.backgroundColor,
      this.bodyWidget,
      this.nameSetting,
      this.messageTextAlign,
      this.titleTextStyle});

  static String name = 'CommonNormalDialog';

  Future<bool?> show() async {
    return await Get.dialog(
      this,
      barrierDismissible: barrierDismissible,
      name: nameSetting ?? CommonNormalDialog.name,
      barrierColor: Colors.black.withOpacity(0.80),
      transitionDuration: const Duration(milliseconds: 250),
    );
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: barrierDismissible,
      child: _buildMain(),
    );
  }

  Widget _buildMain() {
    return Dialog(
      insetPadding: EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      child: Container(
        margin: margin ?? EdgeInsets.symmetric(horizontal: 44.w),
        padding: padding ?? EdgeInsets.symmetric(vertical: 24.w, horizontal: 16.w),
        decoration: decoration ??
            BoxDecoration(
              borderRadius: BorderRadius.circular(36.w),
              color: backgroundColor ?? Colors.white,
            ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (title.isNotEmpty) ...[
              Text(
                title,
                style: titleTextStyle ??
                    TextStyle(
                      color: AppColor.color282828,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.w600,
                      height: 1.5,
                    ),
              ),
              8.verticalSpaceFromWidth,
            ],
            bodyWidget ??
                Text(
                  message ?? "",
                  textAlign: messageTextAlign ?? TextAlign.center,
                  style: TextStyle(
                    color: AppColor.color282828,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                    height: 1.5,
                  ),
                ).marginOnly(bottom: 20.w),
            Row(
              children: [
                Visibility(
                  visible: showCancel,
                  child: Expanded(
                    child: (cancelWidget ??
                            InkWell(
                              onTap: () {
                                if (autoDismiss) {
                                  Get.back();
                                }
                                onCancel?.call();
                              },
                              child: Container(
                                height: 44.w,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(24.w),
                                    border: Border.all(width: .5, color: AppColor.color999999)),
                                child: Text(
                                  cancelText ?? LocaleKeys.cancel.tr,
                                  style: TextStyle(
                                    color: AppColor.color999999,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                            ))
                        .marginOnly(right: 12.w),
                  ),
                ),
                Expanded(
                  child: sureWidget ??
                      CommonButton(
                        height: 44.w,
                        title: sureText ?? LocaleKeys.confirm.tr,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w600,
                        enable: true,
                        onClick: () {
                          if (autoDismiss) {
                            Get.back(result: true);
                          }
                          onSure?.call();
                        },
                      ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
